package com.bzframework.pro.api.core.encipher;

import cn.hutool.core.util.RandomUtil;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author bingzhong
 * @Date 2025/3/5 17:46
 * @Description
 */
@Getter
@Setter
@ConfigurationProperties(prefix = "openapi.client")
@NoArgsConstructor
public class OpenApiClient {

    private String accessKey = "abcd";

    private String secretKey = "abcd";

    public OpenApiClient(String accessKey, String secretKey) {
        this.accessKey = accessKey;
        this.secretKey = secretKey;
    }

    public Map<String,String> getSignHeaderMap(){
        Map<String,String> headerMap = new HashMap<>(4);
        headerMap.put(SignHeaders.X_ACCESS_KEY.getText(), accessKey);
        headerMap.put(SignHeaders.X_NONCE.getText(), RandomUtil.randomString(8));
        String timestamp = String.valueOf(System.currentTimeMillis());
        headerMap.put(SignHeaders.X_TIMESTAMP.getText(), timestamp);
        String sign = SignUtils.genSign(timestamp, secretKey);
        headerMap.put(SignHeaders.X_SIGN.getText(), sign);
        return headerMap;
    }

    public Map<String, List<String>> getSignHeaderMapValueList(){
        Map<String,List<String>> headerMap = new HashMap<>(4);
        headerMap.put(SignHeaders.X_ACCESS_KEY.getText(), Collections.singletonList(accessKey));
        headerMap.put(SignHeaders.X_NONCE.getText(), Collections.singletonList(RandomUtil.randomString(8)));
        String timestamp = String.valueOf(System.currentTimeMillis());
        headerMap.put(SignHeaders.X_TIMESTAMP.getText(), Collections.singletonList(timestamp));
        String sign = SignUtils.genSign(timestamp, secretKey);
        headerMap.put(SignHeaders.X_SIGN.getText(), Collections.singletonList(sign));
        return headerMap;
    }

}
